﻿
(function ($) {
    var currentline = "";
    var isclear;
    var isnodeclick
    var isarrowclick
    var isstart;
    var closeline = "";
    var CKfunction = function ($target, settings) {
        isnodeclick = settings.isnodeclick;
        isarrowclick = settings.isarrowclick;
        if (isnodeclick == 0 && isarrowclick == 0) {

            $target.find('.leftEar, .topEar, .rightEar, .bottomEar').each(function () {
                var $interface = $(this);

                $interface.click(function () {
                    isclear = 1;
                    $pageEditorMainBox = $(".pageEditorMainBox");
                    $("#container").css("background", 'white')
                    if (!currentline) {

                        
                        currentline = line($target);
                        currentline.updateInterface($interface);
                        $("#container").setonwrite();

                        

                        var closelines = function () {
                            $("#container").unbind('mousemove');
                            $("#container").css("background", '#eee')
                            if (!isstart) {
                                currentline.end($target);
                            }
                            else {
                                currentline.start($target);
                            }
                            $("#container").setoffwrite();
                            isstart = null;
                            $target.attrFunctions('l', currentline);
                            $target.findremoveFunction();
                            currentline = null;
                            document.oncontextmenu = "";
                            isarrowclick = 0;
                            isarrowclick = 0;
                            return false;
                        };
                        $("#container").bind('mousemove',
                     function (e) {
                         x = e.pageX;
                         y = e.pageY - $pageEditorMainBox.get(0).offsetTop;
                         currentline.updateEndPoint(x - 5, y - 5);
                         document.oncontextmenu = closelines;

                     });
                     }
                    else {
                        $("#container").css("background", '#eee')
                        $("#container").unbind('mousemove');
                        document.oncontextmenu = "";
                        if (!isstart) { currentline.end($target); }
                        else {
                            currentline.start($target);
                        }
                        $("#container").setoffwrite();
                        isstart = null;
                        //更换成div
                        $target.attrFunctions('l', currentline);
                        currentline = null;
                        $target.findremoveFunction();
                        //currentline.addline()
                        isarrowclick = 0;
                        isarrowclick = 0;
                        addlinstate();
                    }

                })
            });
            $target.click(function () {

                if (isclear == 0) {
                    if (currentline) {
                        $("#container").css("background", '#eee')
                        $("#container").unbind('mousemove');
                        if (!isstart) { currentline.end($target); }
                        else {
                            currentline.start($target);
                        }
                        $("#container").setoffwrite();
                        isstart = null;
                        //更换成div
                        $target.attrFunctions('l', currentline);
                        //currentline.addline();
                        currentline = null;
                        $target.findremoveFunction();
                        document.oncontextmenu = "";
                        isarrowclick = 0;
                        isarrowclick = 0;
                        addlinstate();
                    }
                }
                isclear = 0;
            });


        }
        else if (isnodeclick == 1) {
            $("#container").css("background", 'white')
            currentline = settings.currentlinetarget;
            isstart = 1;
            isnodeclick = 0
            document.oncontextmenu = function () {
                $("#container").unbind('mousemove');

                currentline.start(currentline.getEnd());
                $("#container").setoffwrite();
                isstart = null;
                $target.attrFunctions('l', currentline);
                $target.findremoveFunction();
                currentline = null;
                document.oncontextmenu = "";
                isarrowclick = 0;
                isarrowclick = 0;
                $("#container").css("background", '#eee')
                return false;
            }
        }
        else if (isarrowclick == 1) {
            $("#container").css("background", 'white')
            currentline = settings.currentlinetarget
            isarrowclick = 0;

            document.oncontextmenu = function () {
                $("#container").unbind('mousemove');

                currentline.end(currentline.getStart());
                 $("#container").setoffwrite();
                isstart = null;
                $target.attrFunctions('l', currentline);
                $target.findremoveFunction();
                currentline = null;
                document.oncontextmenu = "";
                isarrowclick = 0;
                isarrowclick = 0;
                $("#container").css("background", '#eee')
                return false;
            }

        }

    };


    jQuery.fn.clickFunction = function (options) {
        settings = jQuery.extend({
            isnodeclick: 0,
            isarrowclick: 0,
            currentlinetarget: null
        }, options);
        CKfunction($(this), settings);
        return this;
    };
})(jQuery);